package org.jmq.myblog.controller;

import org.jmq.myblog.pojo.Article;
import org.jmq.myblog.pojo.MyPage;
import org.jmq.myblog.service.ArticleService;
import org.jmq.myblog.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping("/article/search")
    public ResponseResult<Map> search(@RequestBody MyPage page) {
        return articleService.search(page);
    }

    @PostMapping("/article/getList")
    public ResponseResult<Map> getArticleList(@RequestBody MyPage page) {
        return articleService.getArticleList(page);
    }

    @GetMapping("/article/allArticle")
    @PreAuthorize("@check.hasAuthority('admin')")
    public ResponseResult<List> allArticle() {
        return articleService.allArticle();
    }

    @PostMapping("/article/add")
    @PreAuthorize("@check.hasAuthority('admin')")
    public ResponseResult add(@RequestBody Article article) {

        if(article.getId()!=null){
            article.setModifytime(new Date());
            return articleService.modifyArticle(article);
        }
        article.setPublishtime(new Date());
        article.setDelflag(0);
        return articleService.addArticle(article);
    }

    @GetMapping("/article/recovery")
    @PreAuthorize("@check.hasAuthority('admin')")
    public ResponseResult recovery(@RequestParam("id") int id) {
        return articleService.recoveryArticle(id);
    }

    @GetMapping("/article/delete")
    @PreAuthorize("@check.hasAuthority('admin')")
    public ResponseResult delete(@RequestParam("id") int id) {
        return articleService.deleteArticle(id);
    }

    @PostMapping("/article/imageadd")
    @PreAuthorize("@check.hasAuthority('admin')")
    public ResponseResult imageadd(MultipartFile file) {

        Date date = new Date();
        SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat hms = new SimpleDateFormat("HH:mm:ss");
        String ymdStr = ymd.format(date);
        String hmsStr = hms.format(date).replace(':','-');

        Map<String,String> map = new HashMap<>();

        try {
            //获取原始文件名
            String originalFilename = file.getOriginalFilename();
            //获取后缀名
            String suffixname = originalFilename.substring(originalFilename.lastIndexOf("."));
            if (suffixname.equals(".jpg") || suffixname.equals(".jpeg") || suffixname.equals(".png")
                    || suffixname.equals(".JPG") || suffixname.equals(".JPEG") || suffixname.equals(".PNG")) {
                //文件上传后重命名
                String Filename = hmsStr + suffixname;
                //Linux储存目录
                File filePath = new File("/www/resources/blog/images/" + ymdStr);
                //Windows储存目录
                //File filePath = new File("C:/Users/asus/Desktop/" + ymdStr);
                //若文件夹不存在,则创建出文件夹
                if (!filePath.exists()) {
                    filePath.mkdirs();
                }
                //创建新的文件夹
                File newFile = new File(filePath, Filename);
                //将文件输出到目标的文件中
                file.transferTo(newFile);
                //将保存的文件路径更新到用户信息headimg中
                String imagePath = "http://www.zljmq.top/images/" + ymdStr + "/" + Filename;
                map.put("path",imagePath);
                return new ResponseResult(200,"success",map);
            } else {
                return new ResponseResult(500,"格式不正确，只能上传jpg、jpeg、png格式的照片");
            }

        } catch (Exception e) {
            System.out.println(e);
            return new ResponseResult(500,"未知错误");
        }
    }
}
